package com.sgb.goods.widget.pop.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.sgb.goods.BR;
import com.sgb.goods.R;
import com.sgb.goods.entity.LogisticsDetailEntity;
import com.sgb.goods.utils.TimeUtils;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.text.DecimalFormat;
import java.util.List;

public class LogisticsDetailAdapter extends BaseRecycleAdapter<LogisticsDetailEntity.Record> {

    private ItemOnClickCallBack listener;

    public LogisticsDetailAdapter(Context context, List<LogisticsDetailEntity.Record> datas) {
        super(context, R.layout.goods_pop_item_logistics_detail, datas);
    }

    @Override
    public void convert(ItemViewHolder holder, LogisticsDetailEntity.Record detailEntity) {
        holder.setBinding(BR.entity, detailEntity);
        if (holder.getLayoutPosition() == datas.size() - 1) {
            holder.setVisible(R.id.last_line, true);
        } else {
            holder.setVisible(R.id.last_line, false);
        }
        if (holder.getLayoutPosition() == 0) {
            holder.getView(R.id.line2).setVisibility(View.INVISIBLE);
        } else {
            holder.getView(R.id.line2).setVisibility(View.VISIBLE);
        }

        TextView delivery_time = holder.getView(R.id.delivery_time);
        TextView driver_info = holder.getView(R.id.driver_info);
        TextView car_num = holder.getView(R.id.car_num);
        TextView log_money = holder.getView(R.id.log_money);
        TextView status = holder.getView(R.id.status);
        if (TextUtils.isEmpty(detailEntity.created)) {
            delivery_time.setText("发货时间 ");
        } else {
            delivery_time.setText("发货时间 " + TimeUtils.formatCustomTimeString(Long.valueOf(detailEntity.created), "yyyy-MM-dd HH:mm:ss"));
        }
        driver_info.setText(detailEntity.name + "  " + detailEntity.phone);
        car_num.setText(detailEntity.carNumber);
        DecimalFormat decimalFormat = new DecimalFormat("#0.00");
        log_money.setText("¥" + decimalFormat.format(detailEntity.money));
        String Status = "";
        switch (detailEntity.state) {
            case 0:
                Status = "待收货";
                status.setTextColor(context.getResources().getColor(R.color.color_5792FD));
                break;
            case 1://冻结中
                Status = "已收货";
                holder.getView(R.id.ll_receiving_details).setVisibility(View.VISIBLE);
                status.setTextColor(context.getResources().getColor(R.color.color_06C764));
                break;
            default:
        }
        status.setText(Status);
        // 收货详情跳转点击监听
        holder.getView(R.id.tv_receiving_details).setOnClickListener(v -> {
            if (null != listener) {
                listener.onItemClicked(detailEntity);
            }
        });
    }

    public void setOnClickListener(ItemOnClickCallBack listener) {
        this.listener = listener;
    }

    public interface ItemOnClickCallBack {
        /**
         * 收货详情接口回调函数
         *
         * @param entity
         */
        void onItemClicked(LogisticsDetailEntity.Record entity);
    }

}
